<?php 
/**
*	文档分区读取
*/
class wordClassAction extends apiAction
{

	public function dataAction()
	{
		$limit  = (int)$this->get('limit','1');
		$loadci = (int)$this->get('loadci','1');
		$fqid   = (int)$this->get('fqid','0');
		$folderid  = (int)$this->get('folderid','0');
		$sort  = $this->get('sort');
		$dir   = $this->get('dir');
		$key   = $this->get('key');
		$dbs   = m('word');
		$crows = $dbs->getworc($this->adminrs);
		$fqids = '0';
		foreach($crows as $k=>$rs){
			$fqids .= ','.$rs['id'].'';
		}
		$order 	 = '`type` desc,`sort` asc,`optdt` desc';
		if($sort && $dir)$order = '`'.$sort.'` '.$dir.'';
		$where 	 = '`fqid` in('.$fqids.')'; //默认显示根目录
		$patharr = array();
		if($fqid>0){
			$where = '`fqid` in('.$fqids.') and `fqid`='.$fqid.' and `folderid`='.$folderid.'';
			$fqrs  = m('worc')->getone($fqid);
			$patharr[] = array(
				'name' => $fqrs['name'],
				'uptype' => $fqrs['uptype'],
				'fqid' => $fqid,
				'folderid' => 0,
				'isup' => $this->quanxshoes($fqrs['upuserid'], $fqrs['optid']),
				'isgl' => $this->quanxshoes($fqrs['guanid'], $fqrs['optid']),
				'isxz' => $this->quanxshoes($fqrs['downuid'], ''),
			);
			if($folderid>0){
				$this->tempapre = array();
				$this->getpathstr($folderid);
				foreach($this->tempapre as $k1=>$rs1){
					$patharr[] = array(
						'name' => $rs1['filename'],
						'fqid' => $rs1['fqid'],
						'folderid' => $rs1['id'],
					);
				}
			}
		}else{
			$patharr[] = array(
				'name' => '所有分区',
				'fqid' => 0,
				'uptype' => '',
				'folderid' => 0,
				'isup' => 0,
				'isgl' => 0,
				'isxz' => 0,
			);
		}
		if($key){
			$key = $this->jm->base64decode($key);
			$patharr[0]['name'] .= '<font color=blue>“'.$key.'”</font>';
			$where.=' and (`filename` like \'%'.$key.'%\' or `optname` like \'%'.$key.'%\' or `shatename` like \'%'.$key.'%\')';
		}
		$barr  = $dbs->getlimit($where, (int)$this->get('page','1'),'*',$order, $limit);
		$rows  = $barr['rows'];
		$total = count($rows);
		foreach($rows as $k=>$rs){
			if($rs['type']=='1'){
				$rows[$k]['downshu'] = $dbs->rows('folderid='.$rs['id'].'');
			}
		}
		$barr = array(
			'rows' => $rows,
			'totalCount'=> $barr['count'],
		);
		
		$barr['success'] = true;
		$barr['rows']    = $rows;
		$barr['fqids']   = $fqids;
		$barr['fqid']    = $fqid;
		$barr['crows']   = $crows;
		$barr['patharr']   = $patharr;
		return $barr;
	}
	
	public function shatedataAction()
	{
		$limit = (int)$this->get('limit','1');
		$sort  = $this->get('sort');
		$dir   = $this->get('dir');
		$atype = $this->get('atype');
		$key   = $this->get('key');
		
		$order = '`optdt` desc';
		if($sort && $dir)$order = '`'.$sort.'` '.$dir.'';
		$where = '1=2';
		
		if($atype=='shateall')$where = m('dept')->replacewhere($this->adminrs, '{shateid,receid,not}');
		if($atype=='shatewfx')$where = '`shaterenid`='.$this->adminid.'';
		if($key){
			$key = $this->jm->base64decode($key);
			$where.=" and (`filename` like '%$key%' or `optname` like '%$key%' or `shateren` like '%$key%')";
		}
		
		$barr  = m('word')->getlimit("`type`=0 and ifnull(`shateid`,'')<>'' and $where", (int)$this->get('page','1'),'*',$order, $limit);
		$rows  = $barr['rows'];
		
		$barr = array(
			'rows' => $rows,
			'totalCount'=> $barr['count'],
		);
		
		$barr['success'] = true;
		$barr['rows']    = $rows;
		return $barr;
	}
	
	private function quanxshoes($str,$opid)
	{
		if(isempt($str) && $opid)$str = 'u'.$opid.'';
		if(isempt($str))return 0;
		$where = m('dept')->getjoinwhere($str, $this->adminrs);
		if(m('users')->rows('`id`='.$this->adminid.' and '.$where)>0)return 1;
		return 0;
	}
	
	private function getpathstr($id)
	{
		$fqrs  = m('word')->getone($id);
		if($fqrs){
			if($fqrs['folderid']>0)$this->getpathstr($fqrs['folderid']);
			$this->tempapre[] = $fqrs;
		}
	}
	
	//创建文件夹
	public function createfolderAction()
	{
		$uarr['fqid'] = (int)$this->post('fqid','0');
		$uarr['folderid'] = (int)$this->post('folderid','0');
		$uarr['filename'] = $this->post('name');
		$uarr['uid'] = $this->adminid;
		$uarr['optdt'] = $this->now;
		$uarr['optname'] = $this->adminname;
		$uarr['type'] = 1;
		$newid = m('word')->insert($uarr);
		return returnsuccess(array(
			'newid' => $newid,
			'fqid'  => $uarr['fqid']
		));
	}
	
	//创建分区
	public function createworcAction()
	{
		$uarr['name'] = $this->post('name');
		$uarr['uid'] = $this->adminid;
		$uarr['optid'] = $this->adminid;
		$uarr['optdt'] = $this->now;
		$uarr['size'] = 1024*1024*1024;
		$uarr['optname']  = $this->adminname;
		$uarr['receid']   = 'u'.$this->adminid;
		$uarr['recename'] = $this->adminname;
		$newid = m('worc')->insert($uarr);
		return returnsuccess(array(
			'newid' => $newid,
			'fqid'  => $uarr['fqid']
		));
	}
	//修改分区人
	public function editworcAction()
	{
		$idar = array('receid','guanid','upuserid','downuid');
		$idaa = array('recename','guanname','upuser','downname');
		$lx   = (int)$this->post('lx','0');
		$id   = (int)$this->post('id','0');
		$uarr[$idar[$lx]] = $this->post('sid');
		$uarr[$idaa[$lx]] = $this->post('sna');
		m('worc')->update($uarr, $id);
		return returnsuccess();
	}
	
	//删除分区
	public function delworcAction()
	{
		$id = (int)$this->post('id');
		if(m('word')->rows('fqid='.$id.'')>0)return returnerror('分区下有文件/文件夹不能删除');
		m('worc')->delete($id);
		return returnsuccess();
	}
	
	//保存文件
	public function savefileAction()
	{
		$fnum 	= $this->post('fnum');
		$uarr['fqid'] 		= (int)$this->post('fqid','0');
		$uarr['folderid'] 	= (int)$this->post('folderid','0');
		$uarr['uid'] 		= $this->adminid;
		$uarr['editnaid'] 	= $this->adminid;
		$uarr['optname'] 	= $this->adminname;
		$uarr['editname'] 	= $this->adminname;
		$uarr['type'] 		= 0;
		$uarr['optdt'] 		= $this->now;
		
		$barr 	= c('rockfile')->getsave($fnum);
		if(!$barr['success'])return $barr;
		$data 	= $barr['data'];
		$dbs	= m('word');
		$xu 	= 0; 
		foreach($data as $k=>$rs){
			$uarr['filename'] = $rs['filename'];
			$uarr['filenum'] = $rs['filenum'];
			$uarr['fileext'] = $rs['fileext'];
			$uarr['filesizecn'] = $rs['filesizecn'];
			$uarr['filesize'] = $rs['filesize'];
			$uarr['thumbpath'] = '';
			if(isset($rs['thumbpath']))$uarr['thumbpath'] = $rs['thumbpath'];
			$dbs->insert($uarr);
			$xu++;
		}
		m('word')->reloadworc();
		return returnsuccess(array(
			'msg' => '成功'.$xu.'个文件'
		));
	}
	
	public function reloadworcAction()
	{
		m('word')->reloadworc();
		return returnsuccess();
	}
	
	//删除文件/文件夹
	public function delwordAction()
	{
		$fqid = (int)$this->post('fqid','0');
		$sid  = c('check')->onlynumber($this->post('id'));
		$dbs  = m('word');
		$whe1 = '`type`=0 and `fqid`='.$fqid.' and `id` in('.$sid.')';
		$frow = $dbs->getall($whe1);
		$fnum = '';
		foreach($frow as $k1=>$rs1)if($rs1['filenum'])$fnum.=','.$rs1['filenum'].'';
		if($fnum)c('rockfile')->filedel(substr($fnum, 1));
		$dbs->delete($whe1);
		$dtos = $this->db->row_count();
		$msg  = '成功删除'.$dtos.'个文件';
	
		$rows = $dbs->getall('`type`=1 and `fqid`='.$fqid.' and `id` in('.$sid.')');
		$wjjd = 0;
		foreach($rows as $k=>$rs){
			$id1= $rs['id'];
			$to = $dbs->rows('`folderid`='.$id1.'');
			if($to>0){
				$msg.=',【'.$rs['filename'].'】有下级目录/文件不可删除';
			}else{
				$dbs->delete($id1);
				$wjjd++;
			}
		}
		if($wjjd>0)$msg.='，删除'.$wjjd.'个文件夹';
		m('word')->reloadworc();
		return returnsuccess('', $msg);
	}
	
	//共享
	public function saveshateAction()
	{
		$fqid = (int)$this->post('fqid','0');
		$xid  = c('check')->onlynumber($this->post('xid','0'));
		$uarr['shateren'] 	= $this->adminname;
		$uarr['shaterenid'] = $this->adminid;
		$uarr['shatename'] 	= $this->post('sna');
		$uarr['shateid'] 	= $this->post('sid');
		m('word')->update($uarr, '`type`=0 and `fqid`='.$fqid.' and `id` in('.$xid.')');
		return returnsuccess();
	}
}